//资源文件加载器，确保canvas在图片资源加载完成后才进行渲染
import {Resources} from "./Resources.js";

export class ResourceLoader {
    constructor() {
        this.map = new Map(Resources);
        this.persent=0
        for (let [key, value] of this.map) {
            const image = wx.createImage();
            image.src = value;
            this.map.set(key, image);
        }
    }

    onLoaded(callback) {
        let loadedCount = 0;
        let _this=this
        let per=Math.ceil(100/this.map.size);
        // console.log(per)
        for (let value of this.map.values()) {
            value.onload = () => {
                loadedCount++;
                _this.persent+=per
                // console.log(_this.persent)
                if (loadedCount >= this.map.size) {
                    callback(this.map);
                }
            }
            
        }
    }
    getLoadingStep(){// 获取加载状态
        return this.persent
    }
    static create() {
        return new ResourceLoader();
    }
}